package com.example.myapplication.Fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.activity.LoadingActivity;
import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.CommentSongAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.CommentSong;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CommentSongFragment extends Fragment {
    private RecyclerView comment_recycler;
    private EditText edit_comment_song;
    private TextView btn_comment_edit;
    private CommentSongAdapter adapter;
    private List<CommentSong.HotCommentsBean> hotCommentsBeanList = new ArrayList<>();
    private RecyclerView.LayoutManager manager;
    private TwinklingRefreshLayout refreshLayout;

    public CommentSongFragment() {

    }

    public static CommentSongFragment newInstance(Bundle bundle) {
        CommentSongFragment fragment = new CommentSongFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_comment_song, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        edit_comment_song = view.findViewById(R.id.edit_comment_song);
        btn_comment_edit = view.findViewById(R.id.btn_comment_edit);
        btn_comment_edit.setEnabled(false);
        btn_comment_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommentSong.HotCommentsBean hotCommentsBean = new CommentSong.HotCommentsBean();
                hotCommentsBean.setContent(edit_comment_song.getText().toString());
                CommentSong.HotCommentsBean.UserBean userBean = new CommentSong.HotCommentsBean.UserBean();
                userBean.setNickname(LoadingActivity.profileBean.getNickname());
                userBean.setAvatarUrl(LoadingActivity.profileBean.getAvatarUrl());
                hotCommentsBean.setUser(userBean);
                hotCommentsBeanList.add(hotCommentsBean);
                adapter.setData(hotCommentsBeanList);
                edit_comment_song.setText("");
                manager.scrollToPosition(adapter.getItemCount() - 1);
            }
        });
        //设置编辑框的hint
        SpannableString ss = new SpannableString("这里还缺一条你的优质评论");//定义hint的值
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(12, true);//设置字体大小 true表示单位是sp
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        edit_comment_song.setHint(new SpannedString(ss));

        edit_comment_song.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(edit_comment_song.getText().toString())){
                    btn_comment_edit.setEnabled(true);
                }
                else{
                    btn_comment_edit.setEnabled(false);
                }
            }
        });


        comment_recycler = view.findViewById(R.id.comment_recycler);
        manager = new LinearLayoutManager(getContext());
        comment_recycler.setLayoutManager(manager);
        adapter = new CommentSongAdapter(getContext());
        comment_recycler.setAdapter(adapter);
        int id = getArguments().getInt("songId");
        loadData(id);


    }

    private void loadData(int id) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service = retrofit.create(MusicApiService.class);

        Call<CommentSong> call = service.getCommentSong(id);
        call.enqueue(new Callback<CommentSong>() {
            @Override
            public void onResponse(Call<CommentSong> call, Response<CommentSong> response) {
                hotCommentsBeanList = response.body().getHotComments();
                adapter.setData(hotCommentsBeanList);
            }

            @Override
            public void onFailure(Call<CommentSong> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }


}
